package nxu.it.movieapi.service.pg;

import nxu.it.api.common.param.PageQueryParam;
import nxu.it.api.common.result.Pageable;
import nxu.it.api.model.Comment;
import nxu.it.api.service.CommentService;
import nxu.it.movieapi.service.pg.dao.CommentPgDao;
import org.jdbi.v3.core.Jdbi;
import org.jdbi.v3.sqlobject.SqlObjectPlugin;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Component
public class CommentServicePgImpl implements CommentService {
    private final Jdbi jdbi;

    public CommentServicePgImpl(@Qualifier("createSecondJdbi") Jdbi jdbi) {
        this.jdbi = jdbi;
        // 确保安装了SqlObjectPlugin插件
        this.jdbi.installPlugin(new SqlObjectPlugin());
    }

    @Override
    @Transactional(readOnly = true)
    public Pageable<Comment> pageByUserId(Integer userId, PageQueryParam pageQueryParam) {
        int pageSize = pageQueryParam.getPageSize();
        int pageNumber = pageQueryParam.getPageNumber();
        int offset = (pageNumber - 1) * pageSize;

        // 使用 onDemand 方法获取 CommentPgDao 的实例
        CommentPgDao commentPgDao = jdbi.onDemand(CommentPgDao.class);
        List<Comment> comments = commentPgDao.findByUserId(userId, pageSize, offset);
        int totalRows = (int) commentPgDao.countByUserId(userId);
        int totalPages = (int) Math.ceil((double) totalRows / pageSize);

        return new Pageable<Comment>() {
            @Override
            public int getTotalRow() {
                return totalRows;
            }

            @Override
            public int getTotalPage() {
                return totalPages;
            }

            @Override
            public int getPageSize() {
                return pageSize;
            }

            @Override
            public int getPageNumber() {
                return pageNumber;
            }

            @Override
            public List<Comment> getList() {
                return comments;
            }
        };
    }

    @Override
    @Transactional(readOnly = true)
    public Pageable<Comment> pageByMovieId(Integer movieId, PageQueryParam pageQueryParam) {
        int pageSize = pageQueryParam.getPageSize();
        int pageNumber = pageQueryParam.getPageNumber();
        int offset = (pageNumber - 1) * pageSize;

        // 使用 onDemand 方法获取 CommentPgDao 的实例
        CommentPgDao commentPgDao = jdbi.onDemand(CommentPgDao.class);
        List<Comment> comments = commentPgDao.findByMovieId(movieId, pageSize, offset);
        int totalRows = (int) commentPgDao.countByMovieId(movieId);
        int totalPages = (int) Math.ceil((double) totalRows / pageSize);

        return new Pageable<Comment>() {
            @Override
            public int getTotalRow() {
                return totalRows;
            }

            @Override
            public int getTotalPage() {
                return totalPages;
            }

            @Override
            public int getPageSize() {
                return pageSize;
            }

            @Override
            public int getPageNumber() {
                return pageNumber;
            }

            @Override
            public List<Comment> getList() {
                return comments;
            }
        };
    }
}
